//

#import "MB_DietViewController.h"
#import "MB_AddDietViewController.h"
#import "MB_DietTableViewCell.h"

@interface MB_DietViewController ()<UITableViewDelegate,UITableViewDataSource>
/** Description*/
@property (nonatomic, strong) UITableView *dietTableView;
/** Description*/
@property (nonatomic, strong) NSMutableArray *dataArray;
/** */
@property (nonatomic, strong) MB_SqlView *sql;
/** 是否弹出添加页面*/
@property (nonatomic, assign) BOOL  isShow;

@end

@implementation MB_DietViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _sql = [[MB_SqlView alloc]initWithData:@"DietData" dataName:@"MoodData" success:^{} fail:^(NSError * _Nullable error) {}];
    [self getData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setnavBatalweeTitle:@"Diet"];
    [self.view addSubview:self.dietTableView];
    [self setBtn];
}
//MARK:
- (void)setBtn {
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(kMainScreenWidth - 50, kMainScreenHeight - 160, 60, 40)];
    [button setTitle:@"Add" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button.titleLabel setFont:FontPFRegular(kRHeight(15))];
    button.layer.cornerRadius = 6;
    [button setBackgroundColor:RGB(186, 85, 211)];
    [button addTarget:self action:@selector(Action) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}
- (void)Action {
    NSLog(@"jkddsff");
    MB_AddDietViewController *vc = [[MB_AddDietViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
//#pragma mark ----------------- tableViewDelegate ----------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cellIdentifier";
    MB_DietTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[MB_DietTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = _dataArray[indexPath.row];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kRHeight(10);
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}
//*
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    DietData *moel = _dataArray[indexPath.section];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //
        [_sql mb_deleteEntity:nil key:[NSString stringWithFormat:@"iD=%@",moel.iD] success:^{[self getData];} fail:^(NSError * _Nullable error) {}];
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"Delete";
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    DietData *model = _dataArray[indexPath.row];
    //
    MB_AddDietViewController *vc = [[MB_AddDietViewController alloc]init];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}
//
#pragma mark ----------------- 懒加载 ----------------
- (UITableView *)dietTableView {
    if (!_dietTableView) {
        _dietTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, UI_NAV_BAR_HEIGHT, kMainScreenWidth, kMainScreenHeight) style:UITableViewStylePlain];
        _dietTableView.dataSource = self;
        _dietTableView.delegate   = self;
        _dietTableView.tableFooterView = [UIView new];
        _dietTableView.rowHeight  = kRHeight(160);
    }
    return _dietTableView;
}
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray new];
    }
    return _dataArray;
}
//MARK:数据获取
- (void)getData {
    //
    [_sql mb_readEntity:nil ascending:NO filterStr:nil success:^(NSArray * _Nonnull results) {
        //
        self.dataArray = [results copy];
        [self.dietTableView reloadData];
        if (self.dataArray.count <= 0 && !self.isShow) {
            self.isShow = YES;
            MB_AddDietViewController *vc = [[MB_AddDietViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    } fail:^(NSError * _Nullable error) {}];
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
- (BOOL)bm_isShowLeftswqrrSetting {
    return NO;
}

@end
